/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fileManager;

import com.jspsmart.upload.SmartUpload;
import dateManager.LookDateBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import loginRegister.LoginBean;

/**
 *
 * @author DELL
 */
public class fileDownServlet extends HttpServlet {

    
    
    
        private ServletConfig config;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        this.config = config;
    }

    final public ServletConfig getServletConfig() {
        return config;
    }
    
    
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet fileDownServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet fileDownServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        
           SmartUpload sup = new SmartUpload();
            sup.initialize(getServletConfig(), request, response);
            sup.setContentDisposition(null);
            
            String downtitle=sup.getRequest().getParameter("downtitle");
            log("log1:"+downtitle);
            try {
                
                 Connection con = null;
            java.sql.Statement stmt = null;
            ResultSet rs = null;
            Class.forName("com.mysql.jdbc.Driver");
            HttpSession session = request.getSession();
            String url = "jdbc:mysql://localhost:3306/person";

            con = DriverManager.getConnection(url, "skyline", "739349");

            stmt = con.createStatement();
            
             String userName = null;
               
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.size() == 0) {
                    response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {

                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();

                    }
                }
            
            String sql = "select * from file where userName='" + userName + "' and title='" + downtitle + "'";
                
               rs = stmt.executeQuery(sql);
                String fileName=null;  
                    while (rs.next()) {
                         
                        fileName=rs.getString("name");
                         log(fileName);
                    }  
            
                     try {
            sup.downloadFile("c:\\upload\\"+fileName);
        } catch (Exception e) {
               log("error:",e);
        }
                
        } catch (Exception e) {
        }

 
          response.sendRedirect("http://localhost:8081/PersonInforMSys/fileManager/fileUp.jsp");   
        
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
